/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import DAL.MarksDAL;
import Entities.TblClass;
import Entities.TblCurriculum;
import Entities.TblExamination;
import Entities.TblMarks;
import Entities.TblSemester;
import Entities.TblStudent;
import Entities.TblSubject;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.ValueChangeEvent;

/**
 *
 * @author CuongNV
 */
@ManagedBean
@RequestScoped
public class MarksManagerManagedBean {

    List<TblCurriculum> listCurriculum;
    List<TblSemester> listSemester;
    List<TblSubject> listSubject;
    List<TblExamination> listExamination;
    List<TblClass> listClass;
    List<TblStudent> listStudent;
    static int curriculumId = 0;
    static int semesterId = 0;
    static int subjectId = 0;
    static int examinationId = 0;
    static int classId = 0;
    static float mark;

    /** Creates a new instance of MarksManagerManagedBean */
    public MarksManagerManagedBean() {
    }

    public List<TblCurriculum> getListCurriculum() {
        try {
            listCurriculum = new BLL.CurriculumBLL().getAll();
            return listCurriculum;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

//    public List<TblSemester> getListSemesterByCurriculumId(int curriculumId) {
//        try {
//            listSemester = new BLL.SemesterBLL().getSemesterByCurriculumId(curriculumId);
//            return listSemester;
//        } catch (Exception ex) {
//            System.out.println("Error: " + ex.getMessage());
//            return null;
//        }
//    }
//    public List<TblSubject> getListSubjectBySemesterId(int semesterId) {
//        try {
//            listSubject = new BLL.SubjectBLL().getSubjectBySemesterId(semesterId);
//            return listSubject;
//        } catch (Exception ex) {
//            System.out.println("Error: " + ex.getMessage());
//            return null;
//        }
//    }
//    public List<TblExamination> getListExaminationBySubjectId(int subjectId) {
//        try {
//            listExamination = new BLL.ExaminationBLL().getExaminationBySubjectId(subjectId);
//            return listExamination;
//        } catch (Exception ex) {
//            System.out.println("Error: " + ex.getMessage());
//            return null;
//        }
//    }
//    public List<TblClass> getListClassBySemesterId(int semesterId) {
//        try {
//            listClass = new BLL.ClassBLL().getClassBySemesterId(semesterId);
//            return listClass;
//        } catch (Exception ex) {
//            System.out.println("Error: " + ex.getMessage());
//            return null;
//        }
//    }
    public List<TblSemester> laySemester(int curId) {
        return listSemester = new BLL.SemesterBLL().getSemesterByCurriculumId(curId);
    }

    public List<TblSubject> laySubject(int semId) {
        return listSubject = new BLL.SubjectBLL().getSubjectBySemesterId(semId);
    }

    public void laySubjectAndClass(int semId) {
        //int semId = (Integer) e.getNewValue();
        listSubject = new BLL.SubjectBLL().getSubjectBySemesterId(semId);
        listClass = new BLL.ClassBLL().getClassBySemesterId(semId);
    }

    public void layExamination(int subId) {
        //int subId = (Integer) e.getNewValue();
        listExamination = new BLL.ExaminationBLL().getExaminationBySubjectId(subId);
    }

    public void layStudent(int classId) {
        //int subId = (Integer) e.getNewValue();
        listStudent = new BLL.StudentBLL().getStudentByClassId(classId);
    }

    public List<TblSemester> getListSemester(int curId) {
        if (curriculumId != 0) {
            listSemester = new BLL.SemesterBLL().getSemesterByCurriculumId(curId);
        }

        return listSemester;
    }

    public void setListSemester(List<TblSemester> listSemester) {
        this.listSemester = listSemester;
    }

    public List<TblSubject> getListSubject() {
        if (semesterId != 0) {
            listSubject = new BLL.SubjectBLL().getSubjectBySemesterId(semesterId);
        }
        return listSubject;
    }

    public void setListSubject(List<TblSubject> listSubject) {
        this.listSubject = listSubject;
    }

    public List<TblExamination> getListExamination() {
        if (subjectId != 0) {
            listExamination = new BLL.ExaminationBLL().getExaminationBySubjectId(subjectId);
        }

        return listExamination;
    }

    public void setListExamination(List<TblExamination> listExamination) {
        this.listExamination = listExamination;
    }

    public List<TblClass> getListClass() {
        if (semesterId != 0) {
            listClass = new BLL.ClassBLL().getClassBySemesterId(semesterId);
        }

        return listClass;
    }

    public void setListClass(List<TblClass> listClass) {
        this.listClass = listClass;
    }

    public List<TblStudent> getListStudent() {
        if (classId != 0) {
            listStudent = new BLL.StudentBLL().getStudentByClassId(classId);
        }
//        listStudent = new BLL.StudentBLL().getStudentByClassId(1);
        return listStudent;
    }

    public void setListStudent(List<TblStudent> listStudent) {
        this.listStudent = listStudent;
    }

    public int getCurriculumId() {
        return curriculumId;
    }

    public void setCurriculumId(int curriculumId) {
        this.curriculumId = curriculumId;
    }

    public int getSemesterId() {
        return semesterId;
    }

    public void setSemesterId(int semesterId) {
        this.semesterId = semesterId;
    }

    public int getSubjectId() {
        return subjectId;
    }

    public void setSubjectId(int subjectId) {
        this.subjectId = subjectId;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }

    public int getExaminationId() {
        return examinationId;
    }

    public void setExaminationId(int examinationId) {
        this.examinationId = examinationId;
    }

    public void viewmark(ComponentSystemEvent event) {

        FacesContext fc = FacesContext.getCurrentInstance();

        UIComponent components = event.getComponent();

        //get textbox1 value
        UIInput uiText1 = (UIInput) components.findComponent("examination");
        String text1 = uiText1.getValue().toString();
//
//		//get textbox2 value
//		UIInput uiText2 = (UIInput)components.findComponent("textbox2");
//		String text2 = uiText2.getLocalValue().toString();



        FacesMessage msg = new FacesMessage("Gia tri cua combobox: ", "exam Id: " + text1);
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);

        //components.getClientId() = textPanel
        fc.addMessage(components.getClientId(), msg);

        //passed to the Render Response phase
        fc.renderResponse();


    }

    public void addMark(int examId, int teacherId, String studentRollNo, float mark){
        TblMarks objMark = new TblMarks();
        objMark.setExamId(examId);
        objMark.setTeacherId(teacherId);
        objMark.setStudentRollNo(studentRollNo);
        objMark.setReTest(0);
        objMark.setMark(mark);
        objMark.setRemark("null");

        MarksDAL markDAL = new MarksDAL();
        markDAL.add(objMark);
    }

    public float getMark() {
        return mark;
    }

    public void setMark(float mark) {
        this.mark = mark;
    }
}
